package thread.wang_bao_ling.c17;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Cache1<K, V> {

    final Map<K, V> m = new HashMap<>();
    final ReadWriteLock rwl = new ReentrantReadWriteLock();
    // 读锁
    final Lock r = rwl.readLock();
    // 写锁
    final Lock w = rwl.writeLock();

    // 读缓存
    V get(K key) {
        r.lock();
        try {
            try {
                Thread.currentThread().sleep(10000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return m.get(key);
        } finally {
            r.unlock();
        }
    }

    // 写缓存
    V put(K key, V v) {
        w.lock();
        try {
            return m.put(key, v);
        } finally {
            w.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Cache1<String,String> cache = new Cache1();
        new Thread(()->{
            cache.put("name", "luobiao");
        }).start();
        Thread.currentThread().sleep(1000);
        new Thread(()->{
            String name=cache.get("name");
            System.out.println(name);
        }).start();
    }

}
